package com.hm.ipay.controller;

import com.hm.ipay.IStatusMessage;
import com.hm.ipay.ServiceException;
import com.hm.ipay.comm.aop.LoggerManage;
import com.hm.ipay.domain.RegisterResult;
import com.hm.ipay.domain.result.ExceptionMsg;
import com.hm.ipay.domain.result.ResponseData;
import com.hm.ipay.domain.result.ResponseDataList;
import com.hm.ipay.entity.GameInstance;
import com.hm.ipay.entity.UserAccount;
import com.hm.ipay.service.AccountService;
import com.hm.ipay.service.GameService;
import com.hm.ipay.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
public class AccountController {
    @Autowired
    AccountService accountService;

    @Autowired
    GameService gameService;

    @RequestMapping(value = "/account_info", method = RequestMethod.POST)
    @LoggerManage(description = "账户信息")
    public ResponseData userAccountInfo(Long id) {
        return accountService.userAccountInfo(id);
    }

    @RequestMapping(value = "/bg/set_account_info", method = RequestMethod.POST)
    @LoggerManage(description = "设置账户信息")
    public ResponseData setAccountInfo(UserAccount account) {
        return accountService.setAccountInfo(account);
    }

    @RequestMapping(value = "/transaction_list", method = RequestMethod.POST)
    @LoggerManage(description = "账户流水")
    public ResponseData tranRecordList(Long userId, Integer transactionType) {
        return accountService.tranRecordList(userId, transactionType);
    }

    @RequestMapping(value = "/bg/transaction_list", method = RequestMethod.POST)
    @LoggerManage(description = "账户流水(后台)")
    public ResponseDataList bgTranRecordList(Integer limit, Integer offset, String userName, Integer transactionType) {
        return accountService.bgTranRecordList(limit, offset, userName, transactionType);
    }

    @RequestMapping(value = "/bg/account_list", method = RequestMethod.POST)
    @LoggerManage(description = "账户列表")
    public ResponseDataList userAccountList(Integer limit, Integer offset, String userName, String tel) {
        return accountService.userAccountList(limit, offset, userName, tel);
    }

    @RequestMapping(value = "/bg/frozen_account", method = RequestMethod.POST)
    @LoggerManage(description = "冻结账户")
    public ResponseData frozenAccount(Long userId) {
        return accountService.frozenAccount(userId);
    }

    @RequestMapping(value = "/bg/account_thaw", method = RequestMethod.POST)
    @LoggerManage(description = "账户解冻")
    public ResponseData accountThaw(Long userId) {
        return accountService.accountThaw(userId);
    }

    @RequestMapping(value = "/bg/account_total", method = RequestMethod.POST)
    @LoggerManage(description = "账户总额")
    public ResponseData accountTotal() {
        return accountService.accountTotal();
    }

    @RequestMapping(value = "/bg/account_relationship", method = RequestMethod.POST)
    @LoggerManage(description = "账户关系")
    public ResponseData accountRelationship(Long userId) {
        return accountService.accountRelationship(userId);
    }

    @RequestMapping(value = "/set_profile_photo", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData setFileUpload(@RequestParam(value = "file", required = false) MultipartFile file, Long userId) {
        if (userId == null) {
            return new ResponseData(ExceptionMsg.ParameterError);
        }
        Map<String, Object> resultMap = null;
        try {
            resultMap = upload(file, "user-profile");
            if (!IStatusMessage.SystemStatus.SUCCESS.getMessage().equals(resultMap.get("result"))) {
                new ResponseData(IStatusMessage.SystemStatus.ERROR.getCode(), (String) resultMap.get("msg"));
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            new ResponseData(IStatusMessage.SystemStatus.ERROR.getCode());
        }
        accountService.setAccountProfile(userId, (String) resultMap.get("path"));
        return new ResponseData(ExceptionMsg.SUCCESS, resultMap);
    }

    @RequestMapping(value = "/set_game_result_pic", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData setGameResultPic(@RequestParam(value = "file", required = false) MultipartFile file, String dirType) {
//        if (gameId == null) {
//            return new ResponseData(ExceptionMsg.ParameterError);
//        }
        if (StringUtils.isEmpty(dirType)) {
            return new ResponseData(ExceptionMsg.ParameterError);
        }
        Map<String, Object> resultMap = null;
        try {
            resultMap = upload(file, dirType);
            if (!IStatusMessage.SystemStatus.SUCCESS.getMessage().equals(resultMap.get("result"))) {
                new ResponseData(IStatusMessage.SystemStatus.ERROR.getCode(), (String) resultMap.get("msg"));
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            new ResponseData(IStatusMessage.SystemStatus.ERROR.getCode());
        }
//        GameInstance instance = new GameInstance();
//        instance.setId(gameId);
//        instance.setConfirmPic((String) resultMap.get("path"));
//        gameService.setGameInstanceInfo(instance);
        return new ResponseData(ExceptionMsg.SUCCESS, resultMap);
    }

    private Map<String, Object> upload(MultipartFile file, String dirType) throws ServiceException {
        Map<String, Object> returnMap = new HashMap<>();
        try {
            if (!file.isEmpty()) {
                Map<String, Object> picMap = accountService.uploadPicture(file, dirType);
                if (IStatusMessage.SystemStatus.SUCCESS.getMessage().equals(picMap.get("result"))) {
                    return picMap;
                } else {
                    returnMap.put("result", IStatusMessage.SystemStatus.ERROR.getMessage());
                    returnMap.put("msg", picMap.get("result"));
                }
            } else {
                //LOGGER.info(">>>>>>上传图片为空文件");
                returnMap.put("result", IStatusMessage.SystemStatus.ERROR.getMessage());
                returnMap.put("msg", IStatusMessage.SystemStatus.ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //throw new ServiceException(IStatusMessage.SystemStatus.ERROR);
        }
        return returnMap;
    }
}
